#include "thread.hpp"
#include <stdio.h>
#include <string.h>
namespace moqi{
Thread:: Thread(ThreadCallback && cb):
    _pthread(0),
    _isRun(false),
    _cb(std::move(cb))
{

}

void Thread::start(){
    if(!_isRun){
        int ret = pthread_create(&_pthread, nullptr, thread_start, this);
        if(ret != 0){
            fprintf(stderr,"线程创建失败，错误代码%s\n",strerror(ret));
            return;
        }
        _isRun = true;
    }
}

void Thread::join(){
    if(_isRun){
        pthread_join(_pthread, NULL);
        _isRun =false;
    }
}

void * Thread::thread_start(void * arg){
    Thread *thread = static_cast<Thread *>(arg);
    if(thread){
        thread->_cb();
    }
    return NULL;
}
}//end of namespace moqi
